package com.csh.szhm.activity;

import com.csh.szhm.R;
import com.csh.szhm.fragment.MainFragmentFour;
import com.csh.szhm.fragment.MainFragmentOne;
import com.csh.szhm.fragment.MainFragmentThree;
import com.csh.szhm.fragment.MainFragmentTwo;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class SzhmMainActivity extends MyBaseActivity {

	RadioGroup mRadioGroup;

	@Override
	protected void init() {
		initContentView(R.layout.activity_main);
		mRadioGroup = (RadioGroup) findViewById(R.id.bottomRg);
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// 限制重复点击，导致动画不规则问题
				for (int i = 0; i < group.getChildCount(); i++) {
					if (group.getChildAt(i).getId() != checkedId) {
						group.getChildAt(i).setEnabled(false);
					}
				}
				changeRadioGroup(checkedId);
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
							mRadioGroup.getChildAt(i).setEnabled(true);
						}
					}
				}, 500);
			}
		});
		mRadioGroup.setEnabled(false);
		changButtom(0);
	}

	private void changeRadioGroup(int checkid) {
		switch (checkid) {
		case R.id.rbOne:
			changButtom(0);
			break;
		case R.id.rbTwo:
			changButtom(1);
			break;
		case R.id.rbThree:
			changButtom(2);
			break;
		case R.id.rbFour:
			changButtom(3);
			break;
		}
	}

	private int mBeforeClickPosition = -1;
	private Fragment[] mListFragment;

	private void changButtom(int i) {
		if (i == mBeforeClickPosition) {
			return;
		}
		if (mListFragment == null) {
			mListFragment = new Fragment[4];
		}
		FragmentTransaction mFragmentTransaction = getSupportFragmentManager()
				.beginTransaction();
		if (mBeforeClickPosition != -1) {
			if (mBeforeClickPosition < i) {
				mFragmentTransaction.setCustomAnimations(R.anim.push_left_in,
						R.anim.push_left_out);
			} else {
				mFragmentTransaction.setCustomAnimations(R.anim.push_right_in,
						R.anim.push_right_out);
			}
		}

		switch (i) {
		case 0:
			if (mListFragment[i] == null) {
				MainFragmentOne mMainPlatformHomeFragment = new MainFragmentOne(
						R.layout.main_one_layout);
				mListFragment[i] = mMainPlatformHomeFragment;
				mFragmentTransaction.add(R.id.main_content, mListFragment[i]);
			} else {
				mFragmentTransaction.show(mListFragment[i]);
			}
			if (mBeforeClickPosition >= 0) {
				mFragmentTransaction.hide(mListFragment[mBeforeClickPosition]);
			}

			break;
		case 1:
			if (mListFragment[i] == null) {
				MainFragmentTwo mMainPlatformHomeFragment = new MainFragmentTwo(
						R.layout.main_two_layout);
				mListFragment[i] = mMainPlatformHomeFragment;
				mFragmentTransaction.add(R.id.main_content, mListFragment[i]);
			} else {
				mFragmentTransaction.show(mListFragment[i]);
			}
			mFragmentTransaction.hide(mListFragment[mBeforeClickPosition]);
			break;
		case 2:
			if (mListFragment[i] == null) {
				MainFragmentThree mMainPlatformHomeFragment = new MainFragmentThree(
						R.layout.main_three_layout);
				mListFragment[i] = mMainPlatformHomeFragment;
				mFragmentTransaction.add(R.id.main_content, mListFragment[i]);
			} else {
				mFragmentTransaction.show(mListFragment[i]);
			}
			mFragmentTransaction.hide(mListFragment[mBeforeClickPosition]);
			break;
		case 3:
			if (mListFragment[i] == null) {
				MainFragmentFour mMainPlatformHomeFragment = new MainFragmentFour(
						R.layout.main_four_layout);
				mListFragment[i] = mMainPlatformHomeFragment;
				mFragmentTransaction.add(R.id.main_content, mListFragment[i]);
			} else {
				mFragmentTransaction.show(mListFragment[i]);
			}
			mFragmentTransaction.hide(mListFragment[mBeforeClickPosition]);
			break;
		}
		mBeforeClickPosition = i;
		mFragmentTransaction.commit();
	}

}
